'
该场景为局外升级的卡片
'

extends PanelContainer

@onready var name_label = $%NameLabel
@onready var description_label = $%DescriptionLabel
@onready var progress_bar: ProgressBar = %ProgressBar
@onready var purchase_button: Button = %PurchaseButton
@onready var progress_label: Label = %ProgressLabel
@onready var count_label: Label = %CountLabel

var m_upgrade: ExternalUpgrade = null#自身对应的局外能力


func _ready() -> void:
	purchase_button.disabled = true
	purchase_button.pressed.connect(on_purchase_pressed)
	pass

#更新升级花费和显示
func update_progress() -> void:
	var current_quantity = GameSaves.get_upgrade_quantity(m_upgrade.id)
	var number = GameSaves.get_quantity("experience_number")
	var cost = m_upgrade.experience_cost + m_upgrade.grow_up * current_quantity
	var percent = min(number / (cost * 1.0), 1)
	progress_label.text = str(int(number)) + "/" + str(cost)
	progress_bar.value = percent
	purchase_button.disabled = percent < 1 || current_quantity >= m_upgrade.max_quantitu
	count_label.text = "x%d" % current_quantity
	if current_quantity >= m_upgrade.max_quantitu:
		purchase_button.text = "Max"
	var num = 10*GameSaves.get_upgrade_quantity(m_upgrade.id)
	description_label.text = m_upgrade.description % num
	pass

#设置局外升级卡片的名字和描述
func set_external_upgrade(upgrade: ExternalUpgrade) -> void:
	m_upgrade = upgrade
	name_label.text = upgrade.title
	var num = 10*GameSaves.get_upgrade_quantity(m_upgrade.id)
	description_label.text = upgrade.description % num
	update_progress()
	pass

#任意升级后更新所有卡片
func on_purchase_pressed() -> void:
	if m_upgrade == null:
		return
	GameSaves.add_external_upgrade(m_upgrade)
	var num = (m_upgrade.experience_cost + m_upgrade.grow_up * \
	(GameSaves.get_upgrade_quantity(m_upgrade.id) - 1))
	GameSaves.on_experience_collected(-num)
	self.get_tree().call_group("meat_upgrade_card","update_progress")
	$AnimationPlayer.play("selected")
	pass
